package com.zlc.workflow.core.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.common.core.process.model.entity.StoneInterfaceConfig;
import com.zlc.common.core.process.model.vo.StoneInterfaceClassVo;
import com.zlc.common.core.process.model.vo.StoneInterfaceConfigVo;
import com.zlc.common.core.process.model.vo.StoneSystemVo;
import com.zlc.common.core.process.service.StoneEventConfigService;
import com.zlc.common.core.process.service.StoneInterfaceClassService;
import com.zlc.common.core.process.service.StoneInterfaceConfigService;
import com.zlc.common.core.process.service.StoneSystemService;
import com.zlc.common.util.AssertUtils;
import com.zlc.common.util.BeanCopyUtils;
import com.zlc.common.util.PlatUtils;
import com.zlc.common.util.wrapper.CollectionHelper;
import com.zlc.workflow.core.system.model.InterfaceClassEntity;
import com.zlc.workflow.core.system.service.InterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class InterfaceServiceImpl implements InterfaceService {

    @Autowired
    private StoneInterfaceClassService stoneInterfaceClassService;
    @Autowired
    private StoneInterfaceConfigService stoneInterfaceConfigService;
    @Autowired
    private StoneSystemService stoneSystemService;
    @Autowired
    private StoneEventConfigService stoneEventConfigService;

    @Override
    public InterfaceClassEntity classTree() {
        InterfaceClassEntity entity = new InterfaceClassEntity();
        entity.setClassId("root");
        entity.setClassName("根目录");
        entity.setOrder(0);
        List<StoneInterfaceClassVo> interfaceClassVos = stoneInterfaceClassService.queryVoList(new LambdaQueryWrapper<>());
        List<StoneInterfaceClassVo> rootClassVos = interfaceClassVos.stream()
                .filter(i -> ObjectUtils.isEmpty(i.getParentClassId())).collect(Collectors.toList());
        for(StoneInterfaceClassVo root : rootClassVos){
            InterfaceClassEntity rootE = BeanCopyUtils.copyObj(root,InterfaceClassEntity.class);
            loadChildClass(rootE,interfaceClassVos);
            entity.getChild().add(rootE);
        }
        CollectionHelper.sortList(entity.getChild(),InterfaceClassEntity::getOrder, CollectionHelper.SortEnum.ASC);
        return entity;
    }

    private void loadChildClass(InterfaceClassEntity classEntity,List<StoneInterfaceClassVo> classVos){
        for(StoneInterfaceClassVo child : classVos){
            if(classEntity.getClassId().equals(child.getParentClassId())){
                InterfaceClassEntity childE = BeanCopyUtils.copyObj(child,InterfaceClassEntity.class);
                loadChildClass(childE,classVos);
                classEntity.getChild().add(childE);
            }
        }
        CollectionHelper.sortList(classEntity.getChild(),InterfaceClassEntity::getOrder, CollectionHelper.SortEnum.ASC);
    }

    @Override
    public void addClass(StoneInterfaceClassVo classVo) {
        classVo.setClassId(PlatUtils.uuid());
        stoneInterfaceClassService.saveVo(classVo);
    }

    @Override
    public void saveClass(StoneInterfaceClassVo classVo) {
        stoneInterfaceClassService.saveVo(classVo);
    }

    @Override
    public void removeClass(String classId) {
        StoneInterfaceClassVo  interfaceClass = stoneInterfaceClassService.getByClassId(classId);
        if(ObjectUtils.isEmpty(interfaceClass)){
            return;
        }
        List<StoneInterfaceConfigVo> configVos = stoneInterfaceConfigService.getByClassId(interfaceClass.getClassId());
        AssertUtils.isTrue(ObjectUtils.isEmpty(configVos),"存在接口，请删除接口后再操作！");
        stoneInterfaceClassService.removeById(interfaceClass.getId());
    }

    @Override
    public List<StoneInterfaceConfigVo> getInterList(String classId) {
        return stoneInterfaceConfigService.getByClassId(classId);
    }

    @Override
    public void addInterfaceConfig(StoneInterfaceConfigVo configVo) {
        checkConfigParam(configVo);
        configVo.setInterId(PlatUtils.uuid());
        stoneInterfaceConfigService.saveVo(configVo);
    }

    private void checkConfigParam(StoneInterfaceConfigVo configVo){
        String classId = configVo.getClassId();
        StoneInterfaceClassVo classVo = stoneInterfaceClassService.getByClassId(classId);
        AssertUtils.isTrue(!ObjectUtils.isEmpty(classVo),"类别错误");
    }

    @Override
    public void saveInterfaceConfig(StoneInterfaceConfigVo configVo) {
        checkConfigParam(configVo);
        stoneInterfaceConfigService.saveVo(configVo);
    }

    @Override
    public void remove(String id) {
        StoneInterfaceConfig config= stoneInterfaceConfigService.getById(id);
        if(ObjectUtils.isEmpty(config)){
            return;
        }
        int count = stoneEventConfigService.getCountByInterId(config.getInterId());
        AssertUtils.isTrue(count <= 0 ,"接口已被使用，请确认");
        stoneInterfaceConfigService.removeById(id);
    }
}
